Ext.define('cceaf.control.storage.CustomerField',{
	extend: 'Ext.form.TriggerField', 
    alias: 'widget.cceafcustomerfield',
    requires: [
        'cceaf.control.storage.SelectCustomerGrid'
    ],
           
	triggerClass : 'x-form-date-trigger',
	
	nameOfCustomerid: null,
	nameOfCustomerno: null,
	nameOfCustomername: null,
	nameOfHelpcode: null,
	
	initComponent : function(){
		cceaf.control.storage.CustomerField.superclass.initComponent.call(this);

        this.addEvents(
            'select'
        );
    },

    initEvents: function() {
    	cceaf.control.storage.CustomerField.superclass.initEvents.call(this);
        this.keyNav = new Ext.KeyNav(this.el, {
            "down": function(e) {
                this.onTriggerClick();
            },
            scope: this,
            forceKeyDown: true
        });
    },
	
    onTriggerClick : function(){
        if(this.disabled){
            return;
        }
        
        this.showSelectWin();
    },
    
    listeners:{
		'focus': function(){
			this.selectText();
		},
		'specialkey': function(obj,e){
			if(e.getKey() == 13){//按Enter
				this.showSelectWin();
			}
			if(e.getKey() == 8){//按Backspace
				this.clearSelectValue();
			}
		}
    },
    
    clearSelectValue: function(){
    	var panel = this.up('panel');
    	
    	if(this.nameOfCustomerid != null && this.nameOfCustomerid != ""){
			panel.down('[name=' + this.nameOfCustomerid + ']').setValue('');
		}
    	if(this.nameOfCustomerno != null && this.nameOfCustomerno != ""){
			panel.down('[name=' + this.nameOfCustomerno + ']').setValue('');
		}
    	if(this.nameOfCustomername != null && this.nameOfCustomername != ""){
			panel.down('[name=' + this.nameOfCustomername + ']').setValue('');
		}
    	if(this.nameOfHelpcode != null && this.nameOfHelpcode != ""){
			panel.down('[name=' + this.nameOfHelpcode + ']').setValue('');
		}
    },
    
    showSelectWin: function(){
    	var selectCustomerWin = Ext.getCmp("_selectCustomerWin");
		if(selectCustomerWin == null){
			selectCustomerWin = new Ext.Window({
	    		id:'_selectCustomerWin',
	        	title: '选择客户',
	        	modal: true,
	        	closeAction: 'hide',
	        	//x: this.getPosition()[0],
	        	//y: this.getPosition()[1],
	        	height: 420,
	        	width: 700,
	        	autoScroll: true,
	        	layout: 'fit',
	        	listeners:{
	        		'show': function(){
	        			var grid = this.down('cceafselectcustomergrid');
	        			grid.down('[name=q_customer_customerno]').focus(true,200);
	        		}
	        	},
	        	items: new cceaf.control.storage.SelectCustomerGrid({
	        		triggerField: this
	        	})
	        	
			});
		}
		else{
			//selectCustomerWin.setPosition(this.getPosition()[0],this.getPosition()[1]);
		}
		selectCustomerWin.show();
		
		var selectCustomerGrid = selectCustomerWin.down('cceafselectcustomergrid');
		selectCustomerGrid.triggerField = this;
    }
});